package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.WasherPattern;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.EventCode;
import com.orhanobut.logger.Logger;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/4.
 */

public class PatternAdapter extends RecyclerView.Adapter<PatternAdapter.ViewHolder> {
    private Context context;
    private List<WasherPattern> patterns;

    public PatternAdapter(Context mContext,List<WasherPattern> list){
        context = mContext;
        patterns = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pattern, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        WasherPattern pattern = patterns.get(position);
        if (pattern.isSelected()){
            holder.arlBg.setBackgroundResource(R.drawable.pattern_select_bg);
        }else {
            holder.arlBg.setBackgroundResource(R.drawable.pattern_bg);
        }
        holder.tvPattern.setText(pattern.getPatternName());
        holder.tvMoney.setText("￥"+pattern.getPatternPrice());
        holder.tvTime.setText(pattern.getPatternTime()+"min");
        holder.arlBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.get().send(EventCode.ON_PATTERN_CLICK,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return patterns.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.tv_pattern)
        TextView tvPattern;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.arl_bg)
        AutoRelativeLayout arlBg;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            AutoUtils.autoSize(itemView);
        }
    }
}
